package noncomunicating.classes;

import java.io.File;
import java.io.IOException;
import java.net.Socket;
import java.util.HashMap;

import noncomunicating.classes.SSI.SSIParser;


import comunicating.classes.*;



public class Request extends Thread{
	private Socket clientSocket;
	private String httpRequest;
	private HashMap<String, String> headerData = new HashMap<String, String>();
	private String fileString;
	private String[] dataType;
	private File file;
	private HashMap<String, String> modifiers = new HashMap<String, String>();
	private int statusCode;
	private byte[] fileContent;
	private String status;
	private boolean headRequest = false;
	private boolean control = false;
	
	public HashMap<String, String> getHeaderData(){
		return headerData;
	}
	
	public void addHeaderDataItem(String key, String object){
		headerData.put(key, object);
	}
	
	public String getHeaderDataItem(String key){
		return headerData.get(key);
	}
	
	public boolean isHeadRequest() {
		return headRequest;
	}

	public void setHeadRequest(boolean headRequest) {
		this.headRequest = headRequest;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public int getStatusCode() {
		return statusCode;
	}

	public void setStatusCode(int statusCode) {
		this.statusCode = statusCode;
	}

	public String[] getDataType() {
		return dataType;
	}
	
	public String getDataType(int index) {
		return dataType[index];
	}
	
	public void setDataType(String[] dataType) {
		this.dataType = dataType;
	}

	
	public byte[] getFileContent() {
		return fileContent;
	}

	public void setFileContent(byte[] fileContent) {
		this.fileContent = fileContent;
	}
	
	public String getFileString() {
		return fileString;
	}

	public void setFileString(String file) {
		this.fileString = file;
	}

	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}

	public HashMap<String, String> getModifiers() {
		return modifiers;
	}

	public void addModifier(String key, String value) {
		this.modifiers.put(key, value);
	}

	public String getHttpRequest() {
		return httpRequest;
	}

	public void setHttpRequest(String httpRequest) {
		this.httpRequest = httpRequest;
	}

	public Socket getClientSocket() {
		return clientSocket;
	}

	public Request(Socket clientSocket) {
		this.clientSocket = clientSocket;
	}
	
	public boolean isControl() {
		return control;
	}

	public void setControl(boolean control) {
		this.control = control;
	}
	
	//important shit below
	
	public void close(){
		try {
			clientSocket.close();
		} catch (IOException e) {
			System.err.println("Failed to close socket.");
			e.printStackTrace();
		}
		
	}
	
	private void endRequest(){
		this.close();
		System.out.println("**************************************** Request Closed ****************************************");
	}
	
	@Override
	public void run() { 
			new Receiver(this).printOutput(ServerLoop.verbose);
			new RequestAnalysis(this).printOutput(ServerLoop.verbose);
			
			if(control){
				new Control(this).printOutput(ServerLoop.verbose);
			}
			else{
				new FileGetter(this).printOutput(ServerLoop.verbose);
			}
			
			if(dataType[1].equalsIgnoreCase("html")){ //only runs this for HTML
				new SSIParser(this).printOutput(ServerLoop.verbose);
			}
			
			new Sender(this).printOutput(ServerLoop.verbose);
			
			endRequest();
	}

	
}
